package com.aaa.mythread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread11tongxinCondition {


    //
    CountDownLatch countDownLatch = new CountDownLatch(1);// 初始值设置为1
    private int i=0;



    public  void odd(){
        // i =1
        // 奇数
        while(i<10){
            //synchronized (obj){

            try {
                if (i % 2 == 1) {
                    System.out.println("当前输出的值是奇数，值是:" + i);
                    i++;
                    countDownLatch.countDown();// -1  最小值是0  计数器中的最小值是0  唤醒所有处于等待中的线程

                } else {
                    // 当前的线程让出去
                    countDownLatch.await();
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {


            }

            }
        //}
    }


    public  void even(){
        // 奇数
        while(i<10) {
            //synchronized (obj){
            try {


                if (i % 2 == 0) {
                    System.out.println("当前输出的值是偶数，值是:" + i);
                    i++;
                    //
                    //obj.notify();
                    countDownLatch.countDown();// 1-1 =0

                } else {
                    // 奇数  等待

                    countDownLatch.await();

                }

            }catch(Exception e){
                e.printStackTrace();
        }finally {

            }
        }
    }

    public static void main(String[] args) {

        MyThread11tongxinCondition myThread9tongxin = new MyThread11tongxinCondition();
        // 匿名内部类
          Runnable runnable = new Runnable() {
              @Override
              public void run() {
                  myThread9tongxin.odd();
              }
          };

        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                myThread9tongxin.even();
            }
        };

        Thread thread = new Thread(runnable);
        Thread thread1 = new Thread(runnable2);
        thread.start();
        thread1.start();
    }
}
